package com.code.demo.ui.common;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.CountDownTimer;
import android.widget.TextView;

public class AuthCodeTimeCountUtil extends CountDownTimer {
	private Activity mActivity;
	private TextView btnAuthCode;
	private boolean clickAgain = true;

	// 在这个构造方法里需要传入三个参数，一个是Activity，一个是总的时间millisInFuture，一个是countDownInterval，然后就是你在哪个按钮上做这个是，就把这个按钮传过来就可以了
	public AuthCodeTimeCountUtil( Activity mActivity,long millisInFuture, long countDownInterval,TextView btnAuthCode) {
		super(millisInFuture, countDownInterval);
		this.mActivity = mActivity;
		this.btnAuthCode =btnAuthCode;
	}

	public AuthCodeTimeCountUtil( Activity mActivity,long millisInFuture, long countDownInterval,TextView btnAuthCode, boolean clickAgain) {
		super(millisInFuture, countDownInterval);
		this.mActivity = mActivity;
		this.btnAuthCode = btnAuthCode;
		this.clickAgain = clickAgain;
	}

	@Override
	public void onTick(long millisUntilFinished) {
		btnAuthCode.setClickable(false);//设置不能点击
		btnAuthCode.setEnabled(false);
		if (clickAgain) {
			btnAuthCode.setText("重新获取(" + millisUntilFinished / 1000 + ")");//设置倒计时时间
		} else {
			btnAuthCode.setText("(" + millisUntilFinished / 1000 + ")");//设置倒计时时间
		}
	}


	@SuppressLint("NewApi")
	@Override
	public void onFinish() {
		if (clickAgain) {
			btnAuthCode.setText("重新获取");
			btnAuthCode.setClickable(true);//重新获得点击
			btnAuthCode.setEnabled(true);
		} else {
			btnAuthCode.setText("");
		}
	}
}
